package com.zksite.nator.common.context;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringApplicationContext implements ApplicationContextAware {

	private static ApplicationContext applicationContext;

	public void setApplicationContext(ApplicationContext context) throws BeansException {
		applicationContext = context;
	}

	public static Object getBean(String name) {
		check();
		return applicationContext.getBean(name);
	}

	public static <T> T getBean(Class<T> clazz) {
		check();
		return applicationContext.getBean(clazz);
	}

	private static void check() {
		if (applicationContext == null) {
			throw new IllegalArgumentException();
		}
	}

}
